package com.yzh.openai.trigger.http;

import com.alibaba.fastjson.JSON;
import com.yzh.openai.domain.tool.model.entity.ExcelReqEntity;
import com.yzh.openai.domain.tool.model.entity.UmlReqEntity;
import com.yzh.openai.domain.tool.service.ToolService;
import com.yzh.openai.trigger.http.dto.ExcelResponseDTO;
import com.yzh.openai.types.common.Constants;
import com.yzh.openai.types.common.Response;
import com.yzh.openai.types.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@RestController
@CrossOrigin("${app.config.cross-origin}")
@RequestMapping("/api/${app.config.api-version}/tool")
public class ToolController {
    @Resource
    private ToolService toolService;

    @PostMapping("/excel")
    public Response<ExcelResponseDTO> doExcel(@RequestParam String content,
                                              @RequestParam String chartType,
                                              @RequestParam("file") MultipartFile file)  {
        // 1.校验
        if (StringUtils.isBlank(content) || StringUtils.isBlank(chartType))
            throw new BusinessException(Constants.ResponseCode.PARAM_ERROR, "输入内容不能为空");
        if (file == null) throw new BusinessException(Constants.ResponseCode.PARAM_ERROR, "文件未上传");
        
        // 2.执行业务
        ExcelReqEntity req = ExcelReqEntity.builder()
                .content(content)
                .chartType(chartType)
                .build();
        String[] res = toolService.analyzeExcel(req, file);

        // 3.响应数据
        ExcelResponseDTO responseDTO = ExcelResponseDTO.builder()
                .chartContent(res[1].trim())
                .textContent(res[2].trim())
                .build();
        log.info("responseDTO:{}", JSON.toJSONString(responseDTO));
        return Response.success(responseDTO);
    }

    @PostMapping("/uml")
    public Response<ExcelResponseDTO> doExcel(@RequestParam String content,
                                              @RequestParam String umlType)  {
        // 1.校验
        if (StringUtils.isBlank(content) || StringUtils.isBlank(umlType))
            throw new BusinessException(Constants.ResponseCode.PARAM_ERROR, "输入内容不能为空");

        // 2.执行业务
        UmlReqEntity req = UmlReqEntity.builder()
                .content(content)
                .umlType(umlType)
                .build();
        String res = toolService.analyzeUml(req);
        log.info("最终结果：" + res);
        // 3.响应数据
        return Response.success(res);
    }
    
    
}
